package com.wing.collection;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author heweiye
 * @date 2021/5/11
 * @description: TODO
 */
public class MyLinkedList<E> implements List<E> {

    /**
     * 首节点
     */
    private Node head;


    /**
     * 尾节点
     */
    private Node last;
    /**
     * 链表长度
     */
    private int size;
    /**
     * 操作增删次数
     */
    private int modCount;

    public MyLinkedList() {
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size > 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    private void linkLast(E e) {
        Node l = last;
        Node newNode = new Node(l, e, null);
        last = newNode;
        if (l == null) {
            head = newNode;
        } else {
            l.next = newNode;
        }
        size++;
        modCount++;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E get(int index) {
        rangeCheck(index);
        return node(index).item;
    }

    @Override
    public E set(int index, E element) {
        return null;
    }

    @Override
    public void add(int index, E element) {
        rangeCheck(index);
        if (index == size) {
            linkLast(element);
        } else {
            linkBefore(node(index), element);
        }
    }

    private void linkBefore(Node<E> node, E element) {
        Node prev = node.prev;
        Node newNode = new Node(prev, element, node);
        if (prev == null) {
            head = newNode;
        } else {
            prev.next = newNode;
            node.prev = newNode;
        }
        size++;
        modCount++;
    }

    private Node<E> node(int index) {
        int mid = size >> 1;
        Node node = null;
        if (index < mid) {
            //从头节点开始遍历
            node = head;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            //从尾节点开始遍历
            node = last;
            for (int i = 0; i < size - index - 1; i++) {
                node = node.prev;
            }
        }
        return node;
    }

    private void rangeCheck(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = node(index);
        E oldElement = node.item;
        node.item = null;

        unLink(node);

        return oldElement;
    }

    private void unLink(Node<E> node) {
        Node prev = node.prev;
        Node next = node.next;
        if (prev != null) {
            prev.next = next;
            node.prev = null;
        } else {
            head = next;
        }
        if (next != null) {
            next.prev = prev;
            node.next = null;
        } else {
            last = prev;
        }
        size--;
        modCount++;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }

    private static class Node<E> {
        private Node prev;
        private E item;
        private Node next;

        public Node(Node prev, E item, Node next) {
            this.prev = prev;
            this.item = item;
            this.next = next;
        }
    }
}
